## FocusBackend - 
## Copyright (C) January 26, 2005 Erick P. Bodine
## email: erick_bodine@comcast.net
## webaddress

## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or any later version.

## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


# NOTE: Always do storage('file', 1) to create the file if it doesn't exist
# NOTE: We always do a db.getas() to return a view


"""
NAME
FocusBackend.py

SYNOPSIS


DESCRIPTION


AUTHOR
Erick P Bodine
"""

import sys
import logging
import FocusExceptions

__author__ = "$Author: ebodine $"
__date__ = "$Date: 2005-03-19 16:06:43 -0700 (Sat, 19 Mar 2005) $"
__version__ = "$Revision: 16 $"

log = logging.getLogger("myfocus")


def importName(moduleName, className):
    """ Dynamically import a module. """
    try:
        module = __import__(moduleName, globals(), locals(), [className])
        return vars(module)[className]
        
    except (ImportError,KeyError), error:
        log.critical("Error getting backend module/class: %s" % str(error))
        sys.exit(1)
    
def getBackend(backendType, moduleName, className, backendDatabase):
    """ A factory function to return our dynamically imported class
    Params:
        backendType = The type of backend
        moduleName = THe name of the module to import
        className = The name of the class to instantiate
        backendDatabase = The path to the backend database file
    """
    log.debug("Instantiating backend of type: %s" % backendType)
    log.debug("Instantiating backend -  module: %s class: %s" % (moduleName, className))
    
    BackendClass = importName(moduleName, className)
    return BackendClass(backendDatabase)
    
    
if __name__ == '__main__':
    
    type_ = "sqlite"
    module = "database"
    class_ = "DatabaseTransaction"
    db = "test_database"
    
    MyClass = getBackend(type_, module, class_, db)
    print MyClass.getProjectsList()
